.486              ;Target processor.  Use instructions for Pentium class machines
.MODEL FLAT, C    ;Use the flat memory model. Use C calling conventions

.CODE             ;Indicates the start of a code segment.


;void OrR(void *R1, const void *R2, unsigned maxX)
	public	OrR
OrR proc \
        uses esi edi, \
	R1:ptr byte, \
        R2:ptr byte, \
        maxX:DWORD

        mov     ecx,[maxX]	; cx=amount of pixels

        mov     edi,[R1]	;
        mov     esi,[R2]	;

	sub	ecx,4
	jl	LoopSimple

		; Processing of 4px block with size 3xDWORD	
LoopPix4:lodsd
	or	[edi],eax
	add	edi,4
	sub	ecx,4
	jae	LoopPix4

			; Simple loop for 0,1,2,3 pixels (works also for more px)
LoopSimple:
	add	ecx,4		; ecx was -4, correct counter
        jz	ToEnd		; array has zero size
LoopPix:lodsb
        or	[edi],al
	inc	edi
	loop	LoopPix
ToEnd:	ret
	
OrR endp


;void AndR(void *R1, const void *R2, unsigned maxX)
	public	AndR
AndR proc \
        uses esi edi, \
	R1:ptr byte, \
        R2:ptr byte, \
        maxX:DWORD

        mov     ecx,[maxX]	; cx=amount of pixels

        mov     edi,[R1]	;
        mov     esi,[R2]	;

	sub	ecx,4
	jl	LoopSimple

		; Processing of 4px block with size 3xDWORD	
LoopPix4:lodsd
	and	[edi],eax	
	add	edi,4
	sub	ecx,4
	jae	LoopPix4

			; Simple loop for 0,1,2,3 pixels (works also for more px)
LoopSimple:
	add	ecx,4		; ecx was -4, correct counter
        jz	ToEnd		; array has zero size
LoopPix:lodsb
        and	[edi],al
	inc	edi
	loop	LoopPix
ToEnd:	ret
	
AndR endp



;void AddLu32u8(unsigned Size, uint32_t *Accu, uint8_t *pAdd)
	public  AddLu32u8
AddLu32u8 proc \
        uses esi, \
        count:DWORD, \
        Accu: ptr byte, \
        pAdd: ptr byte \
        
        mov	esi, [pAdd]
        or	esi,esi
        jz	ToEnd
        mov	edx, [Accu]
        or	edx,edx
        jz	ToEnd
        mov	ecx, [count]
        jecxz	ToEnd

ByteLop:movzx	eax,byte ptr [esi]
	inc	esi
	add	[edx],eax
	add	edx,4
	loop	ByteLop
ToEnd:
        ret                     ; _cdecl return        
        
AddLu32u8 endp



;void SubLu32u8(unsigned Size, uint32_t *Accu, uint8_t *pSub)
	public  SubLu32u8
SubLu32u8 proc \
        uses esi, \
        count:DWORD, \
        Accu: ptr byte, \
        pSub: ptr byte \
        
        mov	esi, [pSub]
        or	esi,esi
        jz	ToEnd
        mov	edx, [Accu]
        or	edx,edx
        jz	ToEnd
        mov	ecx, [count]
        jecxz	ToEnd

ByteLop:movzx	eax,byte ptr [esi]
        inc	esi
	sub	[edx],eax
	add	edx,4
	loop	ByteLop
ToEnd:
        ret                     ; _cdecl return        
        
SubLu32u8 endp


	end

